﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.ProfessionalTeam.DAL;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Text;
using Yipusoft.BasicBase.Model;
using Yipusoft.BasicBase.BLL;

public partial class MapInfo_ProfessionalTeam_DeviceNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.UrlReferrer != null)
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
            BindCodeItemList(DeviceClassList, "ZBQC010000");
            bindStateList();
            BindDevice();
            BindDeversInfo();
            ListPageContral1.SetFirstPage();
            BindAllDeviceds(0);

        }
    }
    void bindStateList()
    {
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='ZBQC030000' ORDER BY ItemID,SeqID");
        rblState.DataSource = items;
        rblState.DataBind();
        rblState.SelectedIndex = 0;
    }
    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindAllDeviceds(PageIndex);
    }

    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }

    protected void DeviceClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDevice();
        BindDeversInfo();
    }

    public string GetCodeItem(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        return info != null ? info.ItemName : "";
    }


    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            #region 赋值
            Guid DeviceId = new Guid(gv.DataKeys[e.Row.RowIndex][0].ToString());
            DeviceInfo device = Device.GetDevice(DeviceId);
            Label DeviceName = e.Row.FindControl("DeviceName") as Label;
            DeviceName.Text = device.DeviceName;
            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;

            CodeItemInfo item = CodeItem.GetCodeItem(device.DeviceClass);
            if (item != null)
            {
                DeviceClass.Text = item.ItemName;
            }
            else
            {
                DeviceClass.Text = "--";
            }
            //DeviceClass.Text = item.ItemName;
            Label State = e.Row.FindControl("State") as Label;
            CodeItemInfo Stateitem = CodeItem.GetCodeItem(device.State);
            if (Stateitem != null)
            {
                State.Text = Stateitem.ItemName;
            }
            else
            {
                State.Text = "--";
            }

            #endregion
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Type = Request["Type"];
        string DepartmentId = Request["DepartmentId"];
        TeamDataContext tdc = new TeamDataContext();
        if (Type == "0")
        {
            HelpDevices device = new HelpDevices();
            device.DepartmentId = DepartmentId;
            device.DeviceId = new Guid(ddlDevice.SelectedValue);
            device.Id = Guid.NewGuid();
            device.Lastdate = DateTime.Now;
            device.UpdataUser = User.Identity.Name;
            device.Count = Convert.ToInt32(DeviceCount.Text);
            tdc.HelpDevices.InsertOnSubmit(device);
            tdc.SubmitChanges();
        }
        else if (Type == "1")
        {
            Guid pid = new Guid(Request["Pid"]);
            ProsDevices device = new ProsDevices();
            device.Count = Convert.ToInt32(DeviceCount.Text);
            device.DeviceId = new Guid(ddlDevice.SelectedValue);
            device.Id = Guid.NewGuid();
            device.LastDate = DateTime.Now;
            device.Pid = pid;
            tdc.ProsDevices.InsertOnSubmit(device);
            tdc.SubmitChanges();
        }
        else if (Type == "2")
        {
            Guid Aid = new Guid(Request["Aid"]);
            AssaultDevices device = new AssaultDevices();
            device.Count = Convert.ToInt32(DeviceCount.Text);
            device.AId = Aid;
            device.Id = Guid.NewGuid();
            device.SetDate = DateTime.Now;
            device.SetUserId = User.Identity.Name;
            device.DeviceId = new Guid(ddlDevice.SelectedValue);
            tdc.AssaultDevices.InsertOnSubmit(device);
            tdc.SubmitChanges();
        }
        BindAllDeviceds(0);
    }

    void BindDevice()
    {

        string DepartmentId = Request["DepartmentId"];
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append(" departmentid='" + DepartmentId + "'");
        CodeItemInfo item = CodeItem.GetCodeItem(DeviceClassList.SelectedValue);
        if (item.ItemLevel == 0)
            selectCommandText.Append(" and left(deviceclass,6)='" + DeviceClassList.SelectedValue.Substring(0, 6) + "'");
        if (item.ItemLevel == 1)
            selectCommandText.Append(" and left(deviceclass,8)='" + DeviceClassList.SelectedValue.Substring(0, 8) + "'");
        if (item.ItemLevel == 2)
            selectCommandText.Append(" and deviceclass='" + DeviceClassList.SelectedValue + "'");
        selectCommandText.Append(" and state='" + rblState.SelectedValue + "' ");
        List<DeviceInfo> list = Device.GetList(selectCommandText.ToString());
        ddlDevice.DataTextField = "DeviceName";
        ddlDevice.DataValueField = "DeviceID";
        ddlDevice.DataSource = list;
        ddlDevice.DataBind();
    }
    protected void ddlDevice_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDeversInfo();
    }

    private void BindDeversInfo()
    {
        if (ddlDevice.SelectedValue != "")
        {
            DeviceInfo info = Device.GetDevice(new Guid(ddlDevice.SelectedValue));
            lblDetail.Visible = true;
            lblDetail.Text = "<a href=javascript:OpenWin('../../BasicBase/DeviceView.aspx?DeviceID=" + ddlDevice.SelectedValue + "');>查看</a>";
            DeviceCount.Text = info.DeviceCount.ToString();
            Unit.Visible = true;
            Unit.Text = GetCodeItem(info.UnitName);
        }
        else
        {
            Unit.Visible = false;
            lblDetail.Visible = false;
            DeviceCount.Text = "";
        }
    }

    void BindAllDeviceds(int PageIndex)
    {
        string Type = Request["Type"];
        string DepartmentId = Request["DepartmentId"];
        TeamDataContext tdc = new TeamDataContext();
        if (Type == "0")
        {
            ListPageContral1.ItemCount = tdc.HelpDevices.Where(o => o.DepartmentId == DepartmentId).Count();
            List<HelpDevices> list = tdc.HelpDevices.Where(o => o.DepartmentId == DepartmentId).Skip<HelpDevices>(PageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize).ToList<HelpDevices>();
            gv.DataSource = list;
            gv.DataBind();
        }
        else if (Type == "1")
        {
            Guid Pid = new Guid(Request["Pid"]);
            ListPageContral1.ItemCount = tdc.ProsDevices.Where(o => o.Pid == Pid).Count();
            List<ProsDevices> list = tdc.ProsDevices.Where(o => o.Pid == Pid).Skip<ProsDevices>(PageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize).ToList<ProsDevices>();
            gv.DataSource = list;
            gv.DataBind();
        }
        else if (Type == "2")
        {
            Guid Aid = new Guid(Request["Aid"]);
            ListPageContral1.ItemCount = tdc.AssaultDevices.Where(o => o.AId == Aid).Count();
            List<AssaultDevices> list = tdc.AssaultDevices.Where(o => o.AId == Aid).Skip<AssaultDevices>(PageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize).ToList<AssaultDevices>();
            gv.DataSource = list;
            gv.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string Type = Request["Type"];
        TeamDataContext tdc = new TeamDataContext();

        if (e.CommandName == "Del")
        {
            Guid id = new Guid(e.CommandArgument.ToString());
            if (Type == "0")
            {
                HelpDevices device = tdc.HelpDevices.Where(o => o.Id == id).FirstOrDefault();
                if (device != null)
                {
                    tdc.HelpDevices.DeleteOnSubmit(device);
                    tdc.SubmitChanges();
                }
            }
            else if (Type == "1")
            {

                ProsDevices device = tdc.ProsDevices.Where(o => o.Id == id).FirstOrDefault();
                if (device != null)
                {
                    tdc.ProsDevices.DeleteOnSubmit(device);
                    tdc.SubmitChanges();
                }
            }
            else if (Type == "2")
            {
                AssaultDevices device = tdc.AssaultDevices.Where(o => o.Id == id).FirstOrDefault();
                if (device != null)
                {
                    tdc.AssaultDevices.DeleteOnSubmit(device);
                    tdc.SubmitChanges();
                }
            }
            BindAllDeviceds(0);
            //Prompt.Text = "提示：删除成功！";
            BindAllDeviceds(ListPageContral1.CurrentPageIndex);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
        BindAllDeviceds(ListPageContral1.CurrentPageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TeamDataContext tdc = new TeamDataContext();
        string DeviceID = gv.DataKeys[e.RowIndex][0].ToString();
        Guid id = new Guid(gv.DataKeys[e.RowIndex][1].ToString());
        string Type = Request["Type"];
        TextBox txtDeviceCount = gv.Rows[e.RowIndex].FindControl("txtDeviceCount") as TextBox;
        //数量的限制
        if (Convert.ToInt32(txtDeviceCount.Text) == 0)
        {
            Prompt.Text = "提示：个数不能为0！";
            return;
        }
        DeviceInfo deviceinfo = Device.GetDevice(new Guid(DeviceID));
        if (Convert.ToInt32(txtDeviceCount.Text) > deviceinfo.DeviceCount)
        {
            Prompt.Text = "提示：个数不能大于此器材的总数！";
            return;
        }
        if (Type == "0")
        {
            HelpDevices device = tdc.HelpDevices.Where(o => o.Id == id).FirstOrDefault();
            if (device != null)
            {
                device.Count = Convert.ToInt32(txtDeviceCount.Text);
                tdc.SubmitChanges();
            }
        }
        else if (Type == "1")
        {

            ProsDevices device = tdc.ProsDevices.Where(o => o.Id == id).FirstOrDefault();
            if (device != null)
            {
                device.Count = Convert.ToInt32(txtDeviceCount.Text);
                tdc.SubmitChanges();
            }
        }
        else if (Type == "2")
        {
            AssaultDevices device = tdc.AssaultDevices.Where(o => o.Id == id).FirstOrDefault();
            if (device != null)
            {
                device.Count = Convert.ToInt32(txtDeviceCount.Text);
                tdc.SubmitChanges();
            }
        }
        gv.EditIndex = -1;
        BindAllDeviceds(ListPageContral1.CurrentPageIndex);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        BindAllDeviceds(ListPageContral1.CurrentPageIndex);
    }
    protected void lblReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }
    protected void rblState_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDevice();
        BindDeversInfo();
    }
}
